﻿using languageSite.ViewModels.Common;
using RestSharp;
using System;

namespace languageSite.Helpers
{
    public class HttpHelper
    {
        public static BDTranslateRes? GetTranslateRes(string url)
        {
            var client = new RestClient(url);
            var request = new RestRequest();
            request.Method = Method.Get;
            request.AddHeader("content-type", "text/html;charset=UTF-8");
            var response = client.Execute(request);

            if (response is null || !response.IsSuccessful || string.IsNullOrEmpty(response.Content))
            {
                return null;
            }
            var res = System.Text.RegularExpressions.Regex.Unescape(response.Content);
            var resObj = Newtonsoft.Json.JsonConvert.DeserializeObject<BDTranslateRes>(res);
            return resObj;
        }
        public static string? GetTranslateString(string url)
        {
            var client = new RestClient(url);
            var request = new RestRequest();
            request.Method = Method.Get;
            request.AddHeader("content-type", "text/html;charset=UTF-8");
            var response = client.Execute(request);

            if (response is null || !response.IsSuccessful || string.IsNullOrEmpty(response.Content))
            {
                return null;
            }
            var res = System.Text.RegularExpressions.Regex.Unescape(response.Content);
            var resObj = Newtonsoft.Json.JsonConvert.DeserializeObject<BDTranslateRes>(res);
            return resObj?.trans_result?.FirstOrDefault()?.dst;
        }
    }
}
